package com.fiberhome.ztelauncher.uilib;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by xc on 2018/3/20.
 * 描述：弃用
 */

public class ContainerLayout extends LinearLayout {
    public final static int BOTTOM = 0;
    public final static int LEFT = 1;
    public final static int TOP = 2;
    public final static int RIGHT = 3;
    private int direction;

    public ContainerLayout(@NonNull Context context) {
        super(context);
    }

    public ContainerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ContainerLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置导航栏位置 0-default-bottom  1-left  2-top  3-right
     *
     * @param direction
     */
    public void setDirection(int direction) {
        this.direction = direction;
        if (direction == BOTTOM || direction == TOP) {
            setOrientation(LinearLayout.HORIZONTAL);
        } else {
            setOrientation(LinearLayout.VERTICAL);
        }
        setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        View view = getChildAt(0);
        view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        int childWidth = view.getMeasuredWidth();
        int childHeight = view.getMeasuredHeight();
        if (direction == BOTTOM) {
            int l = (getWidth() - childWidth) / 2;
            int t = getHeight() - childHeight;
            int r = l + childWidth;
            int b = getHeight();
            view.layout(l, t, r, b);
        } else if (direction == LEFT) {
            int l = 0;
            int t = (getHeight() - childHeight) / 2;
            int r = childWidth;
            int b = t + childHeight;
            view.layout(l, t, r, b);
        } else if (direction == TOP) {
            int l = (getWidth() - childWidth) / 2;
            int t = 0;
            int r = l + childWidth;
            int b = childHeight;
            view.layout(l, t, r, b);
        } else if (direction == RIGHT) {
            int l = getWidth() - childWidth;
            int t = (getHeight() - childHeight) / 2;
            int r = getWidth();
            int b = t + childHeight;
            view.layout(l, t, r, b);
        }

        View view2 = getChildAt(1);
        view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        int childWidth2 = view.getMeasuredWidth();
        int childHeight2 = view.getMeasuredHeight();
        if (direction == BOTTOM) {
            int l = 0;
            int t = 0;
            int r = getWidth();
            int b = getHeight() - view.getMeasuredHeight() - 5;
            view2.layout(l, t, r, b);
        } else if (direction == LEFT) {
            int l = view.getMeasuredWidth() + 5;
            int t = 0;
            int r = getWidth();
            int b = getHeight();
            view2.layout(l, t, r, b);
        } else if (direction == TOP) {
            int l = 0;
            int t = childHeight + 5;
            int r = getWidth();
            int b = getHeight();
            view2.layout(l, t, r, b);
            Log.i("TAG", "l = " + l + "\nt=" + t + "\nr=" + r + "\nb=" + b);
        } else if (direction == RIGHT) {
            int l = 0;
            int t = 0;
            int r = getWidth() - view.getMeasuredWidth() - 5;
            int b = getHeight();
            view2.layout(l, t, r, b);
        }
    }
}
